package com.railway.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.railway.bean.Admin;
import com.railway.dao.AdminDao;
import com.railway.util.DBUtil;

public class AdminDaoImpl implements AdminDao {

	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	@Override
	public int updateAdmin(int admin_id, String new_admin_password) {
		String sql = "update Admin set admin_password = ? where admin_id = ?";
		return jt.update(sql, new_admin_password,admin_id);
	}


	@Override
	public String selectAdminPassword(int admin_id) {
		String sql = "select * from Admin where admin_id = ?";
		List<Admin> li = jt.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class),admin_id);
		if(li.size()>0) {
			return li.get(0).getAdmin_password();
		}else {
			return null;
		}
	}


	@Override
	public List<Admin> selectAdmin(String admin_name,String password) {
		String sql = "select * from Admin where admin_name = ? and admin_password =?";
		List<Admin> li = jt.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class),admin_name,password);
		if(li.size()>0) {
			return li;
		}else {
			return null;
		}
	}


	@Override
	public int selectAdminByName(String admin_name) {
		String sql = "select * from Admin where admin_name = ?";
		List<Admin> li = jt.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class),admin_name);
		if(li.size()>0) {
			return li.size();
		}else {
			return 0;
		}
	}


	@Override
	public List<Admin> selectAllAdmin(int pageNum, int pageSize) {
		String sql = "select * from admin limit ?,?";
		return jt.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class),(pageNum-1)*pageSize,pageSize);
	}


	@Override
	public int selectCountAdmin() {
		String sql = "select count(1) from admin";
		return jt.queryForObject(sql, Integer.class);
	}


	@Override
	public int deleteAdmin(int adminId) {
		String sql = "delete from admin where admin_id = ?";
		return jt.update(sql,adminId);
	}


	@Override
	public int updateAdminAll(int adminId, String adminName, String adminPassword) {
		String sql = "update Admin set admin_name = ?, admin_password = ? where admin_id = ?";
		return jt.update(sql, adminName,adminPassword,adminId);
	}


	@Override
	public int insertAdmin(Admin admin) {
		String sql = "insert into admin(admin_name,admin_password) values(?,?)";
		return jt.update(sql,admin.getAdmin_name(),admin.getAdmin_password());
	}


	@Override
	public List<Admin> selectByadminName(String AdminName,int pageNum,int pageSize) {
		String sql = "select * from admin where admin_name = ? limit ?,?";
		List<Admin> adminList = jt.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class),AdminName,(pageNum-1)*pageSize,pageSize);
		return adminList;
	}


	@Override
	public int selectCountAdminName(String AdminName) {
		String sql = "select count(1) from admin where admin_name = ? ";
		return jt.queryForObject(sql, Integer.class,AdminName);
	}

}
